package org.dromara.system.service.impl.pay;

import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.BusinessException;
import org.dromara.system.constant.MachineConstant;
import org.dromara.system.domain.ZfbConfig;
import org.dromara.system.domain.bo.InitPaySimOrderBo;
import org.dromara.system.domain.bo.RenewSimBo;
import org.dromara.system.service.IZfbConfigService;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class AliPayH5 extends AbstractPayService {

    private final static String RE_NEW_NOTIFY_URL = "https://sim.hbtuding.com/system/renewRecords/renewSimWxCallBack";
    private final IZfbConfigService zfbConfigService;

    @Override
    public String initPaySimOrder(InitPaySimOrderBo initPaySimOrderBo) {
        BusinessException.isTrue(Boolean.TRUE, "暂未开通支付宝付款");
        return null;
    }

    @Override
    public String initPayReNewOrder(RenewSimBo bo) {
        AlipayTradeWapPayResponse response = null;
        try {
            AlipayTradeWapPayRequest requestH5 = new AlipayTradeWapPayRequest();
            ZfbConfig zfbConfig = zfbConfigService.getById(MachineConstant.NUMBER.ONE_L);
            //构建请求参数
            JSONObject bizContent = new JSONObject();
            bizContent.put("out_trade_no", bo.getOrderNo());
            bizContent.put("total_amount", bo.getAmount());
            bizContent.put("subject", "图钉科技");
            bizContent.put("product_code", "QUICK_WAP_WAY");
            String orderPay = bizContent.toString();
            requestH5.setNotifyUrl(RE_NEW_NOTIFY_URL);
            requestH5.setBizContent(orderPay);
            AlipayClient alipayClient = getAlipayClient(zfbConfig);
            response = alipayClient.pageExecute(requestH5, "GET");
        } catch (Exception e) {
            log.error("支付宝支付失败{}", e.getMessage());
            BusinessException.isTrue(Boolean.TRUE, "支付宝拉起支付异常！");
        }
        if (response != null && response.isSuccess()) {
            log.info("调用成功{}", response.getBody());
            return response.getBody();
        } else {
            BusinessException.isTrue(Boolean.TRUE, "支付宝拉起支付异常！");
        }
        return null;
    }

    public AlipayClient getAlipayClient(ZfbConfig one) throws AlipayApiException {
        AlipayClient alipayClient = null;
        /** 初始化 **/
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        /** 支付宝网关 **/
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        /** 应用id，如何获取请参考：https://opensupport.alipay.com/support/helpcenter/190/201602493024 **/
        certAlipayRequest.setAppId(one.getAppId());
        /** 应用私钥, 如何获取请参考：https://opensupport.alipay.com/support/helpcenter/207/201602471154?ant_source=antsupport  **/
        certAlipayRequest.setPrivateKey(one.getPrivateKey());
        /** 应用公钥证书路径，下载后保存位置的绝对路径  **/
        certAlipayRequest.setCertPath(one.getCertPath());
        /** 支付宝公钥证书路径，下载后保存位置的绝对路径 **/
        certAlipayRequest.setAlipayPublicCertPath(one.getAlipayPublicCertPath());
        /** 支付宝根证书路径，下载后保存位置的绝对路径 **/
        certAlipayRequest.setRootCertPath(one.getRootCertPath());
        /** 设置签名类型 **/
        certAlipayRequest.setSignType("RSA2");
        /** 设置请求格式，固定值json **/
        certAlipayRequest.setFormat("json");
        /** 设置编码格式 **/
        certAlipayRequest.setCharset("UTF-8");
        alipayClient = new DefaultAlipayClient(certAlipayRequest);
        return alipayClient;
    }
}
